package server;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Main
{
	public static void main(String[] args) throws IOException
	{
		int port = 31415;
		ServerSocket servers = null;
        try {
 		   servers = new ServerSocket(port);
 		} catch (IOException e) {
 		    System.out.println("Could not listen on port: " + port);
 		    System.exit(-1);
 		}
		Scanner console = new Scanner(System.in);
		byte[] ipAddr = null;
        try {
            InetAddress addr = InetAddress.getLocalHost();
            ipAddr = addr.getAddress();
        } catch (UnknownHostException e) {}
        String ipAddress = ipParse(ipAddr);
        System.out.println("-----------------------------");
        System.out.println("Stock Market Server - IP Address: " + ipAddress + "\n\n");
		boolean ever = true;
		for(;ever;)
		{
			Socket client = servers.accept();
			HandlingClients cthread = new HandlingClients(client);
		    cthread.start();
		}
	}
	public static String ipParse(byte[] array)
	{
		int i = 0;
		int[] octs = new int[4];
		while(i<4)
		{
			if(array[i] < 0)
			{
				octs[i] = 256 + array[i];
			}
			else if(array[i] >= 0)
			{
				octs[i] = array[i];
			}
			i = i + 1;
		}
		String address = octs[0] + "." + octs[1] + "." + octs[2] + "." + octs[3];
		return address;
	}
}
